define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config;


    var $delivery = $('[data-toggle="delivery"]');
    if ($delivery.length > 0) {
        var $valuation_checked = $('[data-toggle="valuation"]:checked');
        var $delivery_list = $('[data-toggle="delivery_list"]');
        var delivery = function() {
            this.way = null;
            if ($valuation_checked.length > 0) {
                this.way = $valuation_checked.data("type")
            };
            this.init();
        };
        delivery.prototype = {
            init: function() {
                var _self = this;
                $delivery.on("click", function() {
                    var data = {};
                    var $this = $(this);
                    data.delivery = $this.data("delivery");
                    data.valuation = _self.valuation();
                    data.custom = null;
                    data.name = $this.data("name");
                    data.normal = {
                        delivery: data.delivery,
                        valid:$('[data-toggle="valuation"]:checked').data("valid"),
                        index: 0
                    }
                    var $deliveryel = $('#delivery_item_' + data.delivery)
                    if (this.checked) {
                        var table = _self.createTable(data);
                    } else {
                        $deliveryel.hide();
                    };
                });
                $(document).on("click", ".js_add_area", function() {
                    var $this = $(this),
                        $panel = $this.closest('div.panel'),
                        $table = $("table:first", $panel),
                        index = $("tbody tr", $panel).length,
                        delivery = $panel.data('delivery');
                    var data = {
                        area: "<span class='js_no_area'><input type='hidden' value='{&quot;110000&quot;:{&quot;checkall&quot;:false},&quot;110100&quot;:{&quot;checkall&quot;:false}}'></span>"
                    }
                    data.normal = {
                        delivery: delivery,
                        index: index
                    }
                    _self.createCustom($table, data);
                });

                $('[data-toggle="valuation"]').on("click", function(e) {
                    var $this = $(this);
                    var done = function() {
                        $this.prop("checked", true);
                        $delivery_list.empty();
                        $delivery.prop("checked", false);
                        _self.way = $this.data("type")
                    };
                    var done_init = function() {
                        e && e.preventDefault();
                        if (_self.way != $this.data("type")) {
                            config.msg.msgbox.confirm(config.lang.confirmChangedelivery, function(r) {
                                r && done();
                            });
                        };
                    };
                    var done_flag = $delivery_list.find("table").length == 0
                    if (done_flag) {
                        done();
                    } else {
                        done_init();
                    }
                })
            },
            createTable: function(data) {
                var delivery_item_name = '#delivery_item_' + data.delivery;
                var $deliveryel = $(delivery_item_name)
                if ($deliveryel.length > 0) {
                    $deliveryel.show();
                } else {
                    $delivery_list.append(template("delivery_setting_tpl", data))
                };
                return $(delivery_item_name).closest('div.panel');

            },
            createCustom: function($table, data) {
                $table.append(template("delivery_area_tpl", data))
            },
            valuation: function() {
                return $('[data-toggle="valuation"]:checked').data();
            }


        }
        new delivery();
        $('form button[type="submit"]').on("click", function() {
            if ($('[data-toggle="delivery"]:checked').length == 0) {
                config.msg.info("至少选择一种运送方式！")
                return false;
            };

        })


    };

})
